package tw.edu.tku.im.maze.database;

import java.io.StringReader;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

import tw.edu.tku.im.maze.MazeMap;
import tw.edu.tku.im.maze.Member;
import tw.edu.tku.im.maze.Packet;
import tw.edu.tku.im.maze.StartGameRep;
import tw.edu.tku.im.maze.StartGameReq;

public class StartGameProcessor extends PacketProcessor {

   @Override
   String acceptName() {
      return "startGame";
   }
   
   private Member[] getMember(int roomID, int teamNumber) {
      try {
         Connection cons = DriverManager.getConnection(address,user,passwd);
         cons.setCatalog(database);
         CallableStatement cstmts = 
               cons.prepareCall("{ call usp_getRoomMember(?,?) }");
         cstmts.setInt(1, roomID);
         cstmts.setInt(2, teamNumber);
         cstmts.execute();
         ResultSet rs = cstmts.getResultSet();
         List<Member> tmp = new ArrayList<Member>();
         while(rs.next()) {
            String userID = rs.getString("userID").trim();
            String username = rs.getString("username").trim();
            tmp.add(new Member(userID, username));
         }
         if(tmp.size()==0) { return null; }
         Member[] ans = new Member[tmp.size()];
         tmp.toArray(ans);
         return ans;
      } catch (SQLException e) {
         e.printStackTrace();
      }
      return null;
   }
   
   private MazeMap getMap(int roomID) {
      try {
         Connection cons = DriverManager.getConnection(address,user,passwd);
         cons.setCatalog(database);
         CallableStatement cstmts = 
               cons.prepareCall("{ call usp_getRoomUserMap(?,?) }");
         cstmts.setInt(1, roomID);
         cstmts.registerOutParameter(2, Types.TINYINT);
         cstmts.execute();
         int mapLv = cstmts.getInt(2);
         return MazeMap.getMazeMap(mapLv);
      } catch (SQLException e) {
         e.printStackTrace();
      }
      return null;
   }

   @Override
   Packet process(Packet packet) {//沒有考慮競賽模式
      StartGameReq req = (StartGameReq)packet;
      int roomID = req.getRoomID();
      MazeMap map = getMap(roomID);
      try {
         Connection con = DriverManager.getConnection(address,user,passwd);
         con.setCatalog(database);
         CallableStatement cstmt = 
             con.prepareCall("{ call usp_startGame(?, ?, ?, ?) }");
         
         cstmt.setInt(1, roomID);
         cstmt.setString(2, req.getUserID());
         cstmt.setInt(3, req.getTicket());
         cstmt.setCharacterStream(4, new StringReader(map.toJSONString()), 
             map.toJSONString().length());
         cstmt.execute();
         Member[] teamOne = getMember(roomID, 1);
         return new StartGameRep(teamOne, map);
      } catch (SQLException e) {
         e.printStackTrace();
      }
      return new StartGameRep(null, map);
   }
   
//   public static void main(String[] args) {
//      final StartGameReq req = new StartGameReq(2, "test123", 1331745897);
//      new StartGameProcessor().process(req); 
//   }

}
